package com.example.demo.web;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class ShellCommand {


    public static List<String> exec(String shellCommand) {
        List<String> strList = new ArrayList<String>();
        try {
            Process process = null;
            if(OSinfo.isLinux()){
                process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shellCommand},null,null);
            }
            else if(OSinfo.isWindows()){
                process = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c",shellCommand},null,null);
            }
            // 输出
            InputStreamReader ir = new InputStreamReader(process.getInputStream(), System.getProperty("sun.jnu.encoding"));
            LineNumberReader input = new LineNumberReader(ir);
            String line;
            process.waitFor();
            while ((line = input.readLine()) != null){
                strList.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strList;
    }

    public static void main(String[] args) {
        System.err.println(ShellCommand.exec("ping 127.0.0.1"));
    }
}
